package com.example.imtao;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.imtao.adapter.RootPagerAdapter;
import com.example.imtao.fragment.FindFragment;
import com.example.imtao.fragment.MainFragment;
import com.example.imtao.fragment.MeFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected LinearLayout mMenuMain;
    protected LinearLayout mMenuFind;
    protected LinearLayout mMenuMe;

    protected MainFragment mMainFragment = new MainFragment();
    protected FindFragment mFindFragment = new FindFragment();
    protected MeFragment mMeFragment = new MeFragment();

    private RootPagerAdapter pagerAdapter;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        setupView();
        initView();
    }

    public void setupView() {
        viewPager = findViewById(R.id.viewPager);

        // 创建适配器
        pagerAdapter = new RootPagerAdapter(this);

        // 添加需要展示的 Fragment
        pagerAdapter.addFragment(new MainFragment());
        pagerAdapter.addFragment(new FindFragment());
        pagerAdapter.addFragment(new MeFragment());

        // 设置适配器
        viewPager.setAdapter(pagerAdapter);

        // 设置页面切换的监听器
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                // 页面切换时调用
                // 更新菜单图标的选中状态
                updateMenuIcon(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        mMenuMain = (LinearLayout) this.findViewById(R.id.menu_main);
        mMenuFind = (LinearLayout) this.findViewById(R.id.menu_find);
        mMenuMe = (LinearLayout) this.findViewById(R.id.menu_me);

        setMenuIconSelected(true, false, false);

        mMenuMain.setOnClickListener(this);
        mMenuFind.setOnClickListener(this);
        mMenuMe.setOnClickListener(this);
    }

    private void updateMenuIcon(int position) {
        LinearLayout menuMain = findViewById(R.id.menu_main);
        LinearLayout menuFind = findViewById(R.id.menu_find);
        LinearLayout menuMe = findViewById(R.id.menu_me);
        Log.d("TAG", String.valueOf(position));
        // 初始化所有图标为非选中状态
        setMenuIconSelected(false, false, false);

        // 根据当前页面的位置设置对应的图标选中状态
        if (position == 0) {
            // 设置首页图标选中
            setMenuIconSelected(true, false, false);
        } else if (position == 1) {
            // 设置发现图标选中
            setMenuIconSelected(false, true, false);
        } else if (position == 2) {
            // 设置我的图标选中
            setMenuIconSelected(false, false, true);
        }
    }

    /*
    * 初始化视图
    */
    public void initView() {

        // 获取管理类
        // 事务添加
        // 提交
        /*this.getSupportFragmentManager().beginTransaction().
                add(R.id.container_content, mMainFragment).
                add(R.id.container_content, mFindFragment).hide(mFindFragment).
                add(R.id.container_content, mMeFragment).hide(mMeFragment).
                commit();

        mMenuMain = (LinearLayout) this.findViewById(R.id.menu_main);
        mMenuFind = (LinearLayout) this.findViewById(R.id.menu_find);
        mMenuMe = (LinearLayout) this.findViewById(R.id.menu_me);

        setMenuIconSelected(true, false, false);

        mMenuMain.setOnClickListener(this);
        mMenuFind.setOnClickListener(this);
        mMenuMe.setOnClickListener(this);*/
    }


    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.menu_main) {
            /*this.getSupportFragmentManager().beginTransaction().
                    show(mMainFragment).
                    hide(mFindFragment).
                    hide(mMeFragment).
                    commit();*/
            viewPager.setCurrentItem(0);
            // 设置首页图标选中
            setMenuIconSelected(true, false, false);
        } else if (view.getId() == R.id.menu_find) {
            /*this.getSupportFragmentManager().beginTransaction().
                    hide(mMainFragment).
                    show(mFindFragment).
                    hide(mMeFragment).
                    commit();*/
            viewPager.setCurrentItem(1);
            // 设置发现图标选中
            setMenuIconSelected(false, true, false);
        } else if (view.getId() == R.id.menu_me) {
            /*this.getSupportFragmentManager().beginTransaction().
                    hide(mMainFragment).
                    hide(mFindFragment).
                    show(mMeFragment).
                    commit();*/
            viewPager.setCurrentItem(2);
            // 设置我的图标选中
            setMenuIconSelected(false, false, true);
        }

    }

    /*
     * 设置菜单图标的选中状态
     */
    private void setMenuIconSelected(boolean isMainSelected, boolean isFindSelected, boolean isMeSelected) {
        ImageView menuMainIcon = findViewById(R.id.menu_main).findViewById(R.id.menu_icon_main);
        ImageView menuFindIcon = findViewById(R.id.menu_find).findViewById(R.id.menu_icon_find);
        ImageView menuMeIcon = findViewById(R.id.menu_me).findViewById(R.id.menu_icon_me);

        // 设置图标选中状态
        menuMainIcon.setSelected(isMainSelected);
        menuFindIcon.setSelected(isFindSelected);
        menuMeIcon.setSelected(isMeSelected);
    }


}